//
//  YHBaseTableView.swift
//  Adorbee
//
//  Created by amovision on 2024/11/27.
//

import UIKit
#if canImport(SnapKit)
import SnapKit
#endif

open class YHBaseTableView<T>: UITableView {
  public var dataArray = [Any]() {
    didSet {
      reloadData()
    }
  }
  // 当数据源是一维数组，想让每个cell单独作为一个Section时，可以设置为true，其他情况为false
  public var isSectionCell: Bool = false
  public var modelArray: [[T]] {
    if let dataList = dataArray as? [T] {
      return isSectionCell ?dataList.map { [$0] }:[dataList]
    }
    if let dataList = dataArray as? [[T]] {
      return dataList
    }
    return []
  }
    
  public func reusableCell<C: YHBaseTableViewCell<T>>(_ section: Int, _ row: Int, type: C.Type? = nil, config: (((cell: C, model: T?)) -> Void)? = nil) -> C {
    let model = model(section, row)
    let cellType = type ?? (registerCells?.first as? C.Type)
    let cell = dequeueReusableCell(named: cellType!)
    if let model { cell.model = model }
    config?((cell, model))
    return cell
  }
    
  public func attach(on vc: YHBaseViewController, _ insets: UIEdgeInsets = .zero) {
    vc.view.addSubview(self)
#if canImport(SnapKit)
    snp.makeConstraints { make in
      make.top.equalTo(vc.navigationBar.snp.bottom).offset(insets.top)
      make.left.equalTo(insets.left)
      make.right.equalTo(-insets.right)
      make.bottom.equalTo(-insets.bottom)
    }
#endif
  }
    
  public func model(_ section: Int, _ row: Int) -> T? {
    dataArray.isEmpty ?nil:modelArray[section][row]
  }
    
  public override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    numOfRows?(section) ?? modelArray[section].count
  }

  public override func numberOfSections(in tableView: UITableView) -> Int {
    numOfSections?() ?? modelArray.count
  }
    
  public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if let cell = cellForRows?(indexPath.section, indexPath.row) {
      return cell
    } else {
      return reusableCell(indexPath.section, indexPath.row)
    }
  }
}

open class YHBaseTableViewCell<T>: UITableViewCell {
  public private(set) lazy var containerView = {
    let v = UIView(color: .tertiarySystemBackground, radius: 8)
    v.clipsToBounds = true
    return v
  }()
  public var eventBlock: ((Int) -> Void)?
  override public init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    selectionStyle = .none
    contentView.backgroundColor = .tertiarySystemBackground
    backgroundColor = .clear
    initContent()
  }

  @available(*, unavailable)
  required public init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

  open func initContent() {}
  open func initModel(_ model: T) {}
  public var model: T! {
    didSet {
      initModel(model)
    }
  }

  open class func cellHeight(_ model: T) -> CGFloat { 0 }
}

public class YHListHeaderLabel: UIView {
  public let label: UILabel
  public init(insets: UIEdgeInsets = .zero, text: String? = nil, font: UIFont, color: UIColor, height: CGFloat) {
    label = UILabel(text, font: font, color: color, lines: 0)
    super.init(frame: [YHScreenW, height].rect)
    addSubview(label)
#if canImport(SnapKit)
    label.snp.makeConstraints { make in
      make.edges.equalTo(insets)
    }
#endif
  }

  @available(*, unavailable)
  required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
}
